package pers.amos.eshop.product.ha.controller;

import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.amos.eshop.product.ha.model.ProductInfo;

/**
 * @author amos wong
 * @create 2020-11-26 4:47 下午
 */
@RestController
@Slf4j
public class EshopProductHaController {

    @RequestMapping("/getProductInfo")
    public ProductInfo getProductInfo(Long productId) {
        ProductInfo productInfo = new ProductInfo(productId, "iphone 12", 5599.9,
                "a.jpg,b.jpg", "iphone12的规格", "售后服务",
                "红色,白色,黑色", "5.5", 1L, "2020-11-14 12:59:05", 3L, null);

        return productInfo;
    }

    /**
     * 演示Hystrix Collapser
     *
     * @param productIds
     * @return
     */
    @RequestMapping("/getProductInfos")
    public String getProductInfos(String productIds) {
        log.info("[getProductInfos接口] 接收到一次请求，请求参数= {}", productIds);

        JSONArray jsonArray = new JSONArray();

        for (String productId : productIds.split(",")) {
            ProductInfo productInfo = new ProductInfo(Long.parseLong(productId), "iphone 12", 5599.9,
                    "a.jpg,b.jpg", "iphone12的规格", "售后服务",
                    "红色,白色,黑色", "5.5", 1L, "2020-11-14 12:59:05", 3L, null);
            jsonArray.add(productInfo);
        }

        return jsonArray.toJSONString();
    }
}
